package com.jeecms.wicool.dao.shops.impl;

import com.google.common.base.Strings;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.shops.DistributionAddressDao;
import com.jeecms.wicool.entity.shops.DistributionAddress;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by cuiyanlei on 15/7/15.
 */
@Repository
public class DistributionAddressDaoImpl extends HibernateBaseDao<DistributionAddress, Integer> implements DistributionAddressDao {

    @Override
    public DistributionAddress save(DistributionAddress bean) {
        getSession().save(bean);
        return bean;
    }

    @Override
    public DistributionAddress deleteById(Integer id) {
        DistributionAddress bean = get(id);
        if (bean != null) {
            getSession().delete(bean);
        }
        return bean;
    }


    @Override
    public Pagination getPage(Integer shopId, String city, String district, String address, Integer pageNo, Integer pageSize) {
        Finder f = Finder.create(" select bean from DistributionAddress bean where 1=1 ");
        if (null != shopId) {
            f.append(" and bean.shopInfo.id=:shopId ").setParam("shopId", shopId);
        }

        if (!Strings.isNullOrEmpty(city)) {
            f.append(" and bean.city like :city ").setParam("city", "%" + city + "%");
        }

        if (!Strings.isNullOrEmpty(district)) {
            f.append(" and bean.district like :district ").setParam("district", "%" + district + "%");
        }

        if (!Strings.isNullOrEmpty(address)) {
            f.append(" and bean.address like :address ").setParam("address", "%" + city + "%");
        }

        return find(f, pageNo, pageSize);
    }

    @Override
    public DistributionAddress findById(Integer id) {
        return get(id);
    }

    @Override
    public List<DistributionAddress> list(Integer shopId, String city, String district, String address) {
        Finder f = Finder.create(" select bean from DistributionAddress bean where 1=1 ");
        if (null != shopId) {
            f.append(" and bean.shopInfo.id=:shopId ").setParam("shopId", shopId);
        }

        if (!Strings.isNullOrEmpty(city)) {
            f.append(" and bean.city like :city ").setParam("city", "%" + city + "%");
        }

        if (!Strings.isNullOrEmpty(district)) {
            f.append(" and bean.district like :district ").setParam("district", "%" + district + "%");
        }

        if (!Strings.isNullOrEmpty(address)) {
            f.append(" and bean.address like :address ").setParam("address", "%" + city + "%");
        }
        return find(f);
    }

    @Override
    protected Class<DistributionAddress> getEntityClass() {
        return DistributionAddress.class;
    }
}
